<?php
/**
 * User: aka
 * Time: 2:39
 */

namespace app\read\controller;

use app\validate\prime\QueryPrime;
use think\facade\Session;
use think\Request;

class Prime
{
    private $userId;

    public function __construct()
    {
        $this->userId = Session::get('user_info.id');
    }

    public function __destruct()
    {
        unset($this->userId);
    }

    public function queryPrime(Request $request)
    {
        $validate = new QueryPrime();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else {
            $queryWeapon = $request->param('query_stuff_name_1');
            $queryWarFrame = $request->param('query_stuff_name_2');
            if (!empty($queryWeapon))
            {
                $primeList = \app\tenno\model\Prime::deleteState(0)
                    ->stuffName($queryWeapon)
                    ->type(1)
                    ->paginate(10)
                    ->each(
                        function($item)
                        {
                            $item->uid = \app\tenno\model\Users::userInfo($item->uid)
                                ->find();
                        }
                    );
                return view('query_weapon', ['weapon_list' => $primeList, 'query_name' => $queryWeapon]);
            }else{
                $primeList = \app\tenno\model\Prime::deleteState(0)
                    ->stuffName($queryWarFrame)
                    ->type(2)
                    ->paginate(10)
                    ->each(
                        function($item)
                        {
                            $item->uid = \app\tenno\model\Users::userInfo($item->uid)
                                ->find();
                        }
                    );
                return view('query_war_frame', ['war_frame_list' => $primeList, 'query_name' => $queryWarFrame]);
            }
        }
    }

    public function queryUserWeapon(Request $request)
    {
        $validate = new QueryPrime();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else {
            $queryWeapon = $request->param('query_stuff_name');
            $primeList = \app\tenno\model\Prime::deleteState(0)
                ->stuffName($queryWeapon)
                ->uid($this->userId)
                ->type(1)
                ->paginate(10);
            return view('query_user_weapon', ['list' => $primeList, 'query_name' => $queryWeapon]);
        }
    }

    public function queryUserWarFrame(Request $request)
    {
        $validate = new QueryPrime();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else {
            $queryWarFrame = $request->param('query_stuff_name');
            $primeList = \app\tenno\model\Prime::deleteState(0)
                ->stuffName($queryWarFrame)
                ->uid($this->userId)
                ->type(2)
                ->paginate(10);
            return view('query_user_war_frame', ['list' => $primeList, 'query_name' => $queryWarFrame]);
        }
    }
}